package com.yc.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 重入锁
 * @author yuancan
 *
 */
public class TestLock {
	
	Lock lock = new ReentrantLock();
	
	public void m1() {
		try {
			lock.lock();
			
//			new Thread(() -> {
//				m2();//同一个锁可重入
//			}).start();
			m3();
			
			for (int i = 0; i < 10; i++) {
				TimeUnit.SECONDS.sleep(1);
				System.out.println("---1-");
			}
			
		} catch(InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		
	}
	
	public void m2() {
		try {
			lock.lock();
			for (int i = 0; i < 10; i++) {
				TimeUnit.SECONDS.sleep(1);
				System.out.println("---2-");
			}
			
		} catch(InterruptedException e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
		
	}
	
	public void m3() {
		try {
			lock.lock();
			System.out.println("----3-");
		}finally {
			lock.unlock();
		}
	}

	public static void main(String[] args) {
		TestLock tk = new TestLock();
		Thread t1 = new Thread(tk::m1, "t1");
//		Thread t2 = new Thread(tk::m2,"t2");
		t1.start();
//		t2.start();
		
	}

}
